<%@ page import="java.util.Map" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.math.RoundingMode" %>
<%@ page import="org.springframework.util.CollectionUtils" %>
<%@ page import="com.engine.kq.biz.KQLeaveRulesComInfo" %>
<%@ page import="com.engine.kq.enums.KqSplitFlowTypeEnum" %>
<%@ page import="com.engine.kq.biz.KQUnitBiz" %>
<%@ page import="com.engine.kq.biz.KQTravelRulesBiz" %>
<%@ page import="com.engine.kq.biz.KQExitRulesBiz" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.engine.qz.util.QzUtil" %><%

    out.println("start." + "<br>");

    String userId = request.getParameter("userId");
    String kqDate = request.getParameter("kqDate");
    RecordSet rs1 = new RecordSet();
    double sjycqss = 0.00;
    QzUtil qzUtil = new QzUtil();
    // 获取加班时长
    Map<String, Object> dailyFlowOverTimeData = qzUtil.getDailyFlowOverTimeDataAllowance(userId, kqDate);
    double workingDayOvertime_4leave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|workingDayOvertime_4leave")));
    workingDayOvertime_4leave = workingDayOvertime_4leave < 0 ? 0 : workingDayOvertime_4leave;
    double restDayOvertime_4leave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|restDayOvertime_4leave")));
    restDayOvertime_4leave = restDayOvertime_4leave < 0 ? 0 : restDayOvertime_4leave;
    double holidayOvertime_4leave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|holidayOvertime_4leave")));
    holidayOvertime_4leave = holidayOvertime_4leave < 0 ? 0 : holidayOvertime_4leave;

    double workingDayOvertime_nonleave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|workingDayOvertime_nonleave")));
    workingDayOvertime_nonleave = workingDayOvertime_nonleave < 0 ? 0 : workingDayOvertime_nonleave;
    double restDayOvertime_nonleave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|restDayOvertime_nonleave")));
    restDayOvertime_nonleave = restDayOvertime_nonleave < 0 ? 0 : restDayOvertime_nonleave;
    double holidayOvertime_nonleave = Util.getDoubleValue(Util.null2String(dailyFlowOverTimeData.get(userId + "|" + kqDate + "|holidayOvertime_nonleave")));
    holidayOvertime_nonleave = holidayOvertime_nonleave < 0 ? 0 : holidayOvertime_nonleave;

    double temp = workingDayOvertime_4leave + restDayOvertime_4leave + holidayOvertime_4leave +
            workingDayOvertime_nonleave + restDayOvertime_nonleave + holidayOvertime_nonleave;

    out.println("temp is:" + temp + "<br>");
    sjycqss = sjycqss + (temp/60);
    out.println("sjycqss 1 is:" + sjycqss + "<br>");

    //先获取到实际打卡时长
    Double signmins = 0.00;
    String acqAttenSql = " select resourceid, attendancemins, kqdate,signmins from kq_format_total where resourceid = " + userId + " and kqdate = '" + kqDate + "'";
    rs1.executeQuery(acqAttenSql);
    if (rs1.next()) {
        signmins = Util.getDoubleValue(Util.null2String(rs1.getString("signmins")));
    }
    out.println("signmins is:" + signmins + "<br>");
    sjycqss = sjycqss + (signmins/60);
    out.println("sjycqss 2 is:" + sjycqss + "<br>");

    KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();

    String sql = " select resourceid, newleavetype, durationrule, sum(duration) as val,belongdate from hrmresource a, " + KqSplitFlowTypeEnum.LEAVE.getTablename() + " b " +
            " where a.id = b.resourceid and belongdate >='" + kqDate + "' and belongdate <='" + kqDate + "' and b.resourceid = " + userId +
            " group by resourceid, newleavetype, durationrule,belongdate ";
    rs1.execute(sql);
    while (rs1.next()) {
        String resourceid = rs1.getString("resourceid");
        String belongdate = rs1.getString("belongdate");
        String newleavetype = rs1.getString("newleavetype");
        String durationrule = rs1.getString("durationrule");
        double value = rs1.getDouble("val") < 0 ? 0 : rs1.getDouble("val");

        double proportion = Util.getDoubleValue(kqLeaveRulesComInfo.getProportion(newleavetype));
        if (KQUnitBiz.isLeaveHour(newleavetype, kqLeaveRulesComInfo)) {//按小时
            if (!KQUnitBiz.isLeaveHour(durationrule)) {
                if (proportion > 0) value = value * proportion;
            }
        } else {//按天
//                    if (KQUnitBiz.isLeaveHour(durationrule)) {
//                        if (proportion > 0) value = value / proportion;
//                    }
        }
        out.println(KqSplitFlowTypeEnum.LEAVE.getTablename() + " value is:" + value + "<br>");
        sjycqss = sjycqss + value;
    }
    out.println("sjycqss 3 is:" + sjycqss + "<br>");

    String[] tables = new String[]{KqSplitFlowTypeEnum.EVECTION.getTablename(),//出差
            KqSplitFlowTypeEnum.OUT.getTablename()};//公出
    for (String table : tables) {
        sql = " select resourceid, durationrule, sum(duration) as val,belongdate from hrmresource a, " + table + " b " +
                " where a.id = b.resourceid and (b.status is null or b.status<>1) and belongdate >='" + kqDate
                + "' and belongdate <='" + kqDate + "' and b.resourceid = " + userId +
                " group by resourceid, durationrule,belongdate ";
        rs1.execute(sql);
        while (rs1.next()) {
            String resourceid = rs1.getString("resourceid");
            String belongdate = rs1.getString("belongdate");
            double value = rs1.getDouble("val");
            String durationrule = rs1.getString("durationrule");

            String minimumUnit = "";//单位类型
            double proportion = 0.00;//换算关系
            if (KqSplitFlowTypeEnum.EVECTION.getTablename().equals(table)) {
                minimumUnit = KQTravelRulesBiz.getMinimumUnit();//单位类型
                proportion = Util.getDoubleValue(KQTravelRulesBiz.getHoursToDay());//换算关系
            } else if (KqSplitFlowTypeEnum.OUT.getTablename().equals(table)) {
                minimumUnit = KQExitRulesBiz.getMinimumUnit();//单位类型
                proportion = Util.getDoubleValue(KQExitRulesBiz.getHoursToDay());//换算关系
            }

            if (KQUnitBiz.isLeaveHour(minimumUnit)) {//按小时
                if (!KQUnitBiz.isLeaveHour(durationrule)) {
                    if (proportion > 0) value = value * proportion;
                }
            } else {//按天
//                        if (KQUnitBiz.isLeaveHour(durationrule)) {
//                            if (proportion > 0) value = value / proportion;
//                        }
            }
            out.println(table + " value is:" + value + "<br>");
            sjycqss = sjycqss + value;
        }
    }

    out.println("sjycqss 4 is:" + sjycqss + "<br>");
%>